package tutorial.main.vista;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import tutorial.main.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


/**
 * Guarda la colexion de Pines que se muestran en el mapa
 * @author iuga
 */
public class MapsOverlayPins extends com.google.android.maps.ItemizedOverlay 
{
	 private Context context;
	 private List<OverlayItem> overlays = new ArrayList<OverlayItem>();
	 
	 public MapsOverlayPins(Drawable marker, Context context) 
	 {
		 super(boundCenterBottom(marker));
	     this.context = context;
	     populate(); // Workaround for null pointer exception with empty list
	 }

	@Override
	protected OverlayItem createItem(int i) 
	{
		return overlays.get(i);
	}

	@Override
	public int size() 
	{
		return overlays.size();
	}
	
	public void addOverlay(OverlayItem overlay) 
	{
		overlays.add(overlay);
	    populate();
	}	

	@Override
	protected boolean onTap(int index) 
	{
		OverlayItem itemClicked = overlays.get(index);
		if(itemClicked!=null)
		{
			Toast toast3 = new Toast(context);
			 
			MapActivity ma = (MapActivity)context;
	        LayoutInflater inflater = ma.getLayoutInflater();
	        View layout = inflater.inflate(R.layout.map_toast_layout,(ViewGroup) ma.findViewById(R.id.layout_root));
	 
	        TextView txtTipoConexion = (TextView)layout.findViewById(R.id.txtTipoConexion);
	        txtTipoConexion.setText(itemClicked.getTitle());
	 
	        TextView txtLocation = (TextView)layout.findViewById(R.id.txtLocation);
	        txtLocation.setText(itemClicked.getSnippet());
	        
	        toast3.setDuration(Toast.LENGTH_LONG);
	        toast3.setView(layout);
	        toast3.show();
	        
			return true;
		}
		return false;
	}
	
	public void clear() 
	{
		overlays.clear();
        // Workaround for another issue with this class:
        setLastFocusedIndex(-1);
        populate();
     }
	
	 
	protected void replaceFirst(OverlayItem item)
	{
		if(!overlays.isEmpty())
		{
			overlays.remove(0);
			
		}
		overlays.add(item);
		populate();
	}
	
	 
}
